
function [f,g] = Q_BLP(theta,K,NN,W)

if nargout == 1
    f = theta(K+NN+1:end,1)' * (W \ theta(K+NN+1:end,1));
else
    g = W \ theta(K+NN+1:end,1);
    f = theta(K+NN+1:end,1)'*g;
    g = sparse([zeros(K+NN,1);2*g]);
end



